
const router = require("express-promise-router")();
const CabinetService = require("../../services/cabinet");
const multer  = require('multer');
const config=require("../../config/config");
const tmp_uploads_dir=config.tmp_uploads_dir;
const upload = multer({ dest: tmp_uploads_dir });

/**
 * @openapi
 * /teacher/cabinet/add:
 *   post:
 *     tags:
 *     - cabinet
 *     summary: 创建柜子
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/add', async function(req, res) {
    let cabinet=req.body;
    const result=await CabinetService.add(cabinet);
    return res.json(result);
});

/**
 * @openapi
 * /teacher/cabinet/remove:
 *   post:
 *     tags:
 *     - cabinet
 *     summary: 删除柜子
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/remove', async function(req, res) {
    let {cabinetId}=req.body;
    console.log('cabinetId',cabinetId)
    const result=await CabinetService.remove(cabinetId);
    return res.json(result);
});



/**
 * @openapi
 * /teacher/cabinet/update  :
 *   get:
 *     tags:
 *     - cabinet
 *     summary: 更新
 *     requestBody:
 *     responses:
 *       200:
 *         description: 返回修改的名单
 */
router.post('/update', async function(req, res) {
    let {cabinetId,cabinet}=req.body;
    const result=await CabinetService.update(cabinetId,cabinet);
    return res.json(result);
});

/**
 * @openapi
 * /teacher/cabinet/addGrids:
 *   post:
 *     tags:
 *     - cabinet
 *     summary: 创建格子
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/addGrids', async function(req, res) {
    let cabinet=req.body;
    const result=await CabinetService.addGrids(cabinet);
    return res.json(result);
});
//添加格子
router.post('/add_grid', async function(req, res) {
    let grid=req.body;
    const result=await CabinetService.addGrid(grid);
    return res.json(result);
});

/**
 * @openapi
 * /teacher/good/import/check_file:
 *   post:
 *     tags:
 *     - name_list
 *     summary: 导入物料第一步，检查excel文件
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: string
 *                 format: binary
 *               nameRowNumber:
 *                 type: number
 *               firstDataRowNumber:
 *                 type: number
 *               fieldsMapping:
 *                 type: object  例如："{\"学号\":\"username\",\"姓名\":\"showName\",\"专业\":\"subject\",\"班级\":\"eduClass\"}"
 *     responses:
 *       200:
 *         description: 返回JSON文件信息
 */
router.post('/import/check_file', upload.single('file'), async function  (req, res){
    let {nameRowNumber=1,firstDataRowNumber=2,fieldsMapping}=req.body;
    if(fieldsMapping){
        fieldsMapping=JSON.parse(fieldsMapping);
    }
    //检查Excel文件是否合乎格式
    const result=await CabinetService.checkExcelFile(req.file.path,nameRowNumber,firstDataRowNumber,fieldsMapping);
    if(result.error){
        return res.status(400).send(result);
    }else{
        return res.json(result);
    }
});


/**
 * @openapi
 * /teacher/good/import/confirm_file:
 *   post:
 *     tags:
 *     - name_list
 *     summary: 导入物料第二步，确认导入excel文件
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               filePath:
 *                 type: string
 *               nameRowNumber:
 *                 type: number
 *               firstDataRowNumber:
 *                 type: number
 *               fieldsMapping:
 *                 type: object
 *               id:
 *                 type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回有几条重复，几条新增
 */
router.post('/import/confirm_file',  async function  (req, res){
    //从客户端取得要导入的文件路径，由check_file给出
    var default_nameRowNumber = 1;
    var default_firstDataRowNumber = 2;
    let {nameRowNumber=default_nameRowNumber,firstDataRowNumber=default_firstDataRowNumber,filePath,fieldsMapping,cabinetId}=req.body;
    // if(fieldsMapping){
    //     fieldsMapping=JSON.parse(fieldsMapping);
    // }
    //导入到数据库
    const result=await CabinetService.importExcelFile(filePath,nameRowNumber,firstDataRowNumber,fieldsMapping,cabinetId);
    res.json(result);
});
//修改柜子的HID
router.post('/changeHID', async function(req, res) {
    let {HID,id}=req.body;
    const result=await CabinetService.changeHID(HID,id);
    return res.json(result);
});




module.exports = router;